﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DHQGHN.DAL.BO;
using DHQGHN.DAL.BO.MappingDB;
using DHQGHN.DAL.Models.MappingDB;
using DHQGHN.PM0.Models.Admin;
using DHQGHN.PM0.Controllers;

namespace DHQGHN.MVC.Controllers
{
    public class ExtractController : BaseController
    {
        private ExtractBO ExtractBO;
        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ExtractController));

        public ActionResult Index()
        {
            try
            {
                ViewBag.ActiveMenus = ViewBag.ActiveMenus == null ? new System.Collections.Hashtable() : ViewBag.ActiveMenus;
                ViewBag.ActiveMenus[241] = "active";
                ViewBag.ActiveMenus[245] = "active";

                ViewBag.DBSource = GetDatabaseSource();
                ViewBag.DBStaging = GetDatabaseStaging();

                return View();
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        public ActionResult Extract()
        {
            try
            {
                ExtractBO = new ExtractBO();
                SDBGetReportFormBO reportBO = new SDBGetReportFormBO();
                List<SDBGetReportForm> lstReports = new List<SDBGetReportForm>();

                long? sessionId = null;

                sessionId = ExtractBO.Extract();
                lstReports = reportBO.GetAllSDBReportBySessionID(sessionId);

                return PartialView("_ReportTable", lstReports);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                return ReturnValue(ex.Message, false);
            }
        }

        public ActionResult ExtractData()
        {
            try
            {
                CreateSqlBO();
                bool result = ExtractBO.ExtractData();
                List<SDBGetReportForm> lstReports = new List<SDBGetReportForm>();
                DAL.Models.Admin.ExtractReport resultReport = new DAL.Models.Admin.ExtractReport();
                DAL.Models.Admin.ExtractReport resultReportSource = new DAL.Models.Admin.ExtractReport();
                if (result)
                {
                    resultReport = ExtractBO.GetReportDestination();
                    resultReportSource = ExtractBO.GetReportSource();
                }

                ExtractReportModel data = new ExtractReportModel(resultReport);
                ExtractReportModel reportSource = new ExtractReportModel(resultReportSource);

                ViewBag.ReportSource = reportSource;
                return PartialView("_Report", data);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                return ReturnValue(ex.Message, false);
            }
        }

        private void CreateSqlBO()
        {
            ExtractBO = new ExtractBO();
        }

        private string GetDatabaseSource()
        {
            try
            {
                string databaseName = "";
                SD_ConfigSourceBO bo = new SD_ConfigSourceBO();
                SD_ConfigSource sdConfigSource = bo.GetLatestConfigSource();
                if (sdConfigSource != null)
                {
                    databaseName = sdConfigSource.DataBaseName;
                }

                return databaseName;
            }
            catch
            {
                throw;
            }
        }

        private string GetDatabaseStaging()
        {
            try { 
            string databaseName = "";
            SD_ConfigDestinationBO bo = new SD_ConfigDestinationBO();
            SD_ConfigDestination sdConfigSource = bo.GetLatestConfigSource();
            if (sdConfigSource != null)
            {
                databaseName = sdConfigSource.DataBaseName;
            }

            return databaseName;
            }
            catch
            {
                throw;
            }
        }
    }
}